<template>
    <view>
        <scroll-view class="list" :scroll-y="true" :refresher-triggered="refresh" :refresher-enabled="true" @refresherrefresh="onRefresh" @scrolltolower="scrollTolower">
            <view class="item" v-for="item in list" :key="item.id">
                <view class="name">
                    {{ item.name }}
                </view>
                <view class="btn">
                    <u-button type="primary" size="small" text="编辑" @click="ToEdit(item.id)"></u-button>
                    <u-button type="error" :customStyle="{marginLeft:'10px'}" size="small" text="删除"></u-button>
                </view>
            </view>

            <u-loadmore :status="status" />
        </scroll-view>
        <u-button type="primary" :customStyle="btnStyle" text="确定"></u-button>
    </view>
</template>
<script>
export default{
    data() {
        return {
            btnStyle:{
                position: 'fixed',
                bottom: 0
            },
            refresh:false,// 下拉刷新状态
            finished:false,// 上拉加载状态
            list:[],
            page:1,
            limit:10,
            LoginAdmin:{},
            status:'loadmore'
        }
    },
    methods:{
        scrollTolower()
        {
            if(this.finished) return;

            this.page = this.page + 1;

            this.status = 'loading';

            this.getSourceData();
        },
        onRefresh()
        {
            this.refresh = true;

            this.page = 1;

            this.list = [];

            this.finished = false;

            this.getSourceData();
        },
        async getSourceData()
        {
            let data = {
                page:this.page,
                limit:this.limit,
                adminid:this.LoginAdmin.id
            }

            let result = await this.$u.api.manage.sourceIndex(data);

            this.refresh = false;

            if(result && result.code === 1)
            {
                this.list = this.list.concat(result.data);
            }else{
                this.finished = true;
                this.status = 'nomore';
                return;
            }
        },
        ToEdit(id)
        {
            this.$u.route('pages/manage/source/edit',{id})
        }
    },
    onReady()
    {
        this.LoginAdmin = uni.getStorageSync('admin') || {};

        this.getSourceData();
    }
}
</script>

<style>
.list{
    height: 92.5vh;
    overflow: hidden;
}

.item{
    display: flex;
    padding: 20px 10px;
    justify-content: space-between;
    border-bottom: 1px solid #ccc;
}

.item .btn{
    display: flex;
}
</style>